//定义全局变量 good
var tytow = document.getElementById('typetwo');
var requestNum = 1;
var tab = "default";
var mask = document.querySelector(".mask");
var searchTemp = location.search;
if(searchTemp) {
	var searchName = localStorage.getItem("searchName");
} else {
	var searchName = "";
}
var orRefresh = true;
var DateV = {
	price: {
		low: '',
		up: ''
	},
	type: '',
	color: '',
	kind: ''
};
//END
//排版

function xuanze() {
	var de = document.getElementById('default');
	var declass = document.querySelector('.default');
	var ps = declass.children;
	var sales = document.getElementById('sales');
	var cselect = document.getElementById('cselect');
	var img = de.querySelector('img');
	//默认选择Tap封装
	var orClick = false;
	Tap(de, function() {
		if(orClick == false) {
			orClick = true;
			declass.style.visibility = 'visible';
			img.style.transform = 'rotate(180deg)';
			img.style.webkitTransform = 'rotate(180deg)';
		} else {
			orClick = false;
			menu();
		}
	});
	//END
	//点击其他，让下拉菜单消失
	function menu() {
		declass.style = '';
		img.style.transform = '';
		img.style.webkitTransform = '';
		declass.style.visibility = 'hidden';
	}
	//默认选择中的默认排序Tap封装
	de.style.color = '#EF5964';
	ps[0].style.color = '#EF5964';
	for(var i = 0; i < ps.length; i++) {
		Tap(ps[i], function(e, dom) {
			for(var o = 0; o < ps.length; o++) {
				ps[o].style.color = '';
			}
			dom.style.color = '#EF5964';
			var html = dom.innerHTML;
			declass.style.visibility = 'hidden';
			img.style.transform = '';
			img.style.webkitTransform = '';
			de.querySelector('em').innerHTML = html;
			de.style.color = '#EF5964';
			sales.style.color = '';
			//ajax请求后台筛选数据
			requestNum = 1;
			orObj = false;
			switch(html) {
				case "默认排序":
					break;
				case "最新发布":
					tab = "now";
					break;
				case "价格最高":
					tab = "price_desc";
					break;
				case "价格最低":
					tab = "price_asc";
					break;
				case "默認排序":
					break;
				case "最新發布":
					tab = "now";
					break;
				case "價格最高":
					tab = "price_desc";
					break;
				case "價格最低":
					tab = "price_asc";
					break;
				default:
					tab = "default";
					break;
			}
			mask.style.display = "block";
			ajax({
				method: 'GET',
				url: pathName + '/api/v1/good',
				data: {
					"tab": tab,
					"page": requestNum,
					"priceLow": DateV.price.low,
					"priceHigh": DateV.price.up,
					"currentShopType": DateV.type,
					"colour": DateV.color,
					"flower_system": DateV.kind,
					"search": searchName
				},
				success: function(response) {
					mask.style.display = "none";
					var obj = JSON.parse(response).data;
					console.log(obj);
					jshtml(obj);
					key = 0;
					orRefresh = true;
				}
			});
			//ajaxEND
		});
	}
	//END
	//销量优先Tap封装
	Tap(sales, function() {
		window.location.href = 'special.html#xian';
	});
	//END
	//筛选模块Tap封装
	var cselectclass = document.querySelector('.out_select');
	var last = cselectclass.querySelector('.last').querySelector('s');
	var lastspan = cselectclass.querySelector('.last').querySelector('span');
	var is = cselectclass.querySelector('.zl').querySelectorAll('i');
	var lis = cselectclass.querySelector('.color').querySelectorAll('li');
	var kind = cselectclass.querySelector('.kind').querySelectorAll('li');
	var inputs = cselectclass.querySelector('.jg').querySelectorAll('input');
	var drak = document.getElementById('drak');
	var i0 = 0,
		li0 = 0;
	li = 0;
	//进入筛选区
	cselectclass.addEventListener("touchmove", function(e) {
		e.preventDefault();
		e.stopPropagation();
	});
	Tap(cselectclass, function(e) {
		e.stopPropagation();
	});
	drak.addEventListener("touchmove", function(e) {
		e.preventDefault();
	});
	Tap(cselect, function(e, dom) {
		menu();
		drak.style.display = "block";
		setTimeout(function() {
			drak.style.opacity = "1";
		}, 100)
		cselectclass.style.width = "85%";
	});
	//重置封装
	Tap(lastspan, function() {
		for(var k = 0; k < is.length; k++) {
			is[k].className = '';
		}
		for(var k0 = 0; k0 < lis.length; k0++) {
			lis[k0].className = '';
		}
		for(var k1 = 0; k1 < kind.length; k1++) {
			kind[k1].className = '';
		}
		inputs[0].value = '';
		inputs[1].value = '';
		DateV = {
			price: {
				low: '',
				up: ''
			},
			type: '',
			color: '',
			kind: ''
		};
		DateV.color = "";
		DateV.price.low = "";
		DateV.price.up = "";
		DateV.type = "";
		DateV.kind = "";

	});
	//取消
	Tap(drak, function() {
		cselectclass.style.width = "0";
		drak.style.opacity = 0;
		setTimeout(function() {
			drak.style.display = "none";
		}, 400);
	});
	//选择确定
	Tap(last, function(e) {
		e.stopPropagation();
		requestNum = 1;
		orObj = true;
		cselectclass.style.width = "0";
		drak.style.opacity = 0;
		setTimeout(function() {
			drak.style.display = "none";
		}, 400);
		DateV.price = {
			low: inputs[0].value,
			up: inputs[1].value
		};
		if((DateV.price || DateV.type || DateV.color || DateV.kind)) {
			sales.style.color = '';
			cselect.style.color = '#EF5964';
			console.log(DateV.price.low + ';' + DateV.price.up + ';' + DateV.type + ';' + DateV.color + ';' + DateV.kind);
			mask.style.display = "block";
			ajax({
				method: 'GET',
				url: pathName + '/api/v1/good',
				data: {
					"tab": tab,
					"page": requestNum,
					"priceLow": DateV.price.low,
					"priceHigh": DateV.price.up,
					"currentShopType": DateV.type,
					"colour": DateV.color,
					"flower_system": DateV.kind,
					"search": searchName
				},
				success: function(response) {
					mask.style.display = "none";
					var obj = JSON.parse(response).data;
					console.log(obj);
					jshtml(obj);
					key = 0;
					orRefresh = true;
				}
			});
			//ajaxEND
		}
	});

	//选择颜色和花种类
	for(; i0 < is.length; i0++) {
		Tap(is[i0], function(e, dom) {
			for(var o0 = 0; o0 < is.length; o0++) {
				is[o0].className = '';
			}
			dom.className = 'red';
			var flowerType = dom.innerHTML;
			switch(flowerType) {
				case "国产花":
					flowerType = 13;
					break;
				case "进口花":
					flowerType = 14;
					break;
				case "國産花":
					flowerType = 13;
					break;
				case "進口花":
					flowerType = 14;
					break;
				default:
					flowerType = -1;
					break;
			}
			DateV.type = flowerType;
		})
	}
	for(; li0 < lis.length; li0++) {
		Tap(lis[li0], function(e, dom) {
			for(var o0 = 0; o0 < lis.length; o0++) {
				lis[o0].className = '';
			}
			dom.className = 'red';
			var flowerColor = dom.innerHTML;
			switch(flowerColor) {
				case "红色":
					flowerColor = 1
					break;
				case "粉色":
					flowerColor = 2;
					break;
				case "黄色":
					flowerColor = 3;
					break;
				case "绿色":
					flowerColor = 4;
					break;
				case "蓝色":
					flowerColor = 5;
					break;
				case "紫色":
					flowerColor = 6;
					break;
				case "白色":
					flowerColor = 7;
					break;
				case "香槟":
					flowerColor = 8;
					break;
					//繁体字
				case "紅色":
					flowerColor = 1
					break;
				case "黃色":
					flowerColor = 3;
					break;
				case "綠色":
					flowerColor = 4;
					break;
				case "藍色":
					flowerColor = 5;
					break;
				default:
					flowerColor = -1;
					break;
			}
			DateV.color = flowerColor;
		})
	}
	for(; li < kind.length; li++) {
		Tap(kind[li], function(e, dom) {
			for(var o0 = 0; o0 < kind.length; o0++) {
				kind[o0].className = '';
			}
			dom.className = 'red';
			var flowerKind = dom.innerHTML;
			switch(flowerKind) {
				case "玫瑰":
					flowerKind = 1
					break;
				case "绣球":
					flowerKind = 2;
					break;
				case "桔梗":
					flowerKind = 3;
					break;
				case "兰花":
					flowerKind = 4;
					break;
				case "百合":
					flowerKind = 5;
					break;
				case "康乃馨":
					flowerKind = 6;
					break;
				case "叶类":
					flowerKind = 7;
					break;
				case "其他":
					flowerKind = 0;
					break;
				case "繡球":
					flowerKind = 2;
					break;
				case "桔梗":
					flowerKind = 3;
					break;
				case "蘭花":
					flowerKind = 4;
					break;
				case "葉類":
					flowerKind = 7;
					break;
				default:
					flowerKind = -1;
					break;
			}
			DateV.kind = flowerKind;
		})
	}
	//END
	var change = document.getElementById('change');
	Tap(change, function() {
		menu();
		var orone = document.getElementById('orone');
		var preClass = orone.getAttribute('class');
		var toaddClass = orone.getElementsByClassName('thetow');
		if(preClass == 'thetowfher') {
			var aftClass = 'theonefher';

		} else {
			var aftClass = 'thetowfher';

		}
		orone.setAttribute('class', aftClass);
	});
};
//筛选模块END
//点击进入详情页
function toDetial() {
	var mod = document.getElementsByClassName('thetow');
	for(var i = 0; i < mod.length; i++) {
		Tap(mod[i], function(e, dom) {
			var chooseId = dom.getAttribute('dataid');
			console.log(chooseId)
			window.location.href = 'detail.html?id=' + chooseId;
		});
	}
}
//登陆模块
function enterCar() {
	var token = selectByKey("like_token");
	var ent = document.querySelector("header .enter");
	var mask = document.querySelector(".markT");
	Tap(ent, function() {
		if(token) {
			window.location.href = "trolley.html";
		} else {
			mask.style.display = "block";
		}
	});
	var sure = document.querySelector("#sureT");
	var cancle = document.querySelector("#cancleT");
	Tap(sure, function() {
		window.location.href = "personal/enter.html";
	});
	Tap(cancle, function() {
		mask.style.display = "none";
	});

}
//ajax模板引擎
function jshtml(data, x) {
	var mainmax = document.getElementById('mainmax');
	var html = '';
	for(var k in data) {
		var htmlm = tytow.innerHTML;
		if (data[k].sort==0) {
			htmlm = htmlm.replace(/\{\{tejia\}\}/,'');
		} else{
		htmlm = htmlm.replace(/\{\{tejia\}\}/,'<img src="../statics/images/online/tejia.png" class="tejia"/>');
		};
		htmlm = htmlm.replace(/\{\{imgsrc\}\}/, data[k].img);
		htmlm = htmlm.replace(/\{\{alt\}\}/, data[k].goodsname);
		htmlm = htmlm.replace(/\{\{h3\}\}/, data[k].goodsname);
		//htmlm = htmlm.replace(/\{\{h5\}\}/, "云南A级花材 uni rose");
		htmlm = htmlm.replace(/\{\{span\}\}/, data[k].price);
		//			htmlm = htmlm.replace(/\{\{i\}\}/, 2324);
		htmlm = htmlm.replace(/\{\{id\}\}/, data[k].id);
		html += htmlm;
	}
	if(x) {
		mainmax.children[0].innerHTML += html;
		toDetial();
		return;
	}
	var mdiv = document.querySelector('#orone');
	mdiv.innerHTML = html;
	mainmax.innerHTML = '';
	mainmax.appendChild(mdiv);
	toDetial();
};
//求购物车里数量
function getNum() {
	var num = document.getElementById("good_num");
	var token = selectByKey("like_token");
	console.log(token)
	if(token) {
		$.ajax({
			url: pathName + "/api/v1/member/cartNum",
			type: "GET",
			headers: {
				"Authorization": "Bearer " + token
			},
			success: function(data) {
				num.innerHTML = "(" + data.cartNum + ")";
			}
		})
	}
}
//END
//点击返回
function toBack() {
	var back = document.querySelector("#back");
	Tap(back, function() {
		if(searchName) {
			localStorage.removeItem("searchName");
			window.location.href = "online.html";
		} else {
			window.location.href = "../index.html";
		}
	});
}
window.onload = function() {
	totop('.TOTOP');
	xuanze();
	enterCar();
	getNum();
	toBack();
	//设置排序查找方式的margin值（兼容华为自带浏览器）
	//var label = document.querySelector("#classify label:nth-child(2)");
	//label.style.margin = ""
	var key = 0;
	var cselect = document.getElementById("cselect");
	if(searchName) {
		var select = document.getElementById("select");
		var inputs = select.querySelector("input");
		var imgM = select.querySelector("img");
		inputs.style.paddingLeft = "8%";
		imgM.style.left = "5%";
		inputs.style.width = "92%";
		inputs.value = searchName;
		cselect.style.display = "none";
	} else {
		cselect.style.display = "inline";
	}
	//页面加载完毕第一次ajax
	ajax({
		method: 'GET',
		url: pathName + '/api/v1/good',
		data: {
			"tab": "default",
			"page": "1",
			"search": searchName
		},
		success: function(response) {
			mask.style.display = "none";
			var obj = JSON.parse(response).data;
			jshtml(obj, 1);
			key = 0;
		}
	});
	//END
	window.onscroll = function() {
		var end = document.querySelector("footer div");
		var t = document.documentElement.scrollTop || document.body.scrollTop;
		//滚动过的高度(屏幕顶到最上面··不包括现在的屏幕高度)
		var y = document.documentElement ? document.documentElement.scrollHeight : document.body.scrollHeight;
		//浏览器滚动条总高
		var w = document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight;
		//屏幕高度
		if(t > 100) {
			document.querySelector('.TOTOP').style.opacity = 1;
		} else {
			document.querySelector('.TOTOP').style.opacity = 0;
		}
		if(t + w > y - 200) {
			if(key == 0 && orRefresh == true) {
				end.innerHTML = "加载中...";
				requestNum++;
				key = 1;
				console.log(DateV.price.low + ';' + DateV.price.up + ';' + DateV.type + ';' + DateV.color + ';' + requestNum);
				ajax({
					method: 'GET',
					url: pathName + '/api/v1/good',
					data: {
						"tab": tab,
						"page": requestNum,
						"priceLow": DateV.price.low,
						"priceHigh": DateV.price.up,
						"currentShopType": DateV.type,
						"flower_system": DateV.kind,
						"colour": DateV.color,
						"search": searchName
					},
					success: function(response) {
						mask.style.display = "none";
						var res = JSON.parse(response);
						response = res.data;
						jshtml(response, 1);
						key = 0;
						if(res.current_page == res.last_page) {
							orRefresh = false;
							end.innerHTML = "已经到底啦";
						}
					}
				});
			}
		}
	};
};